﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using EOS.DataModel;
using EOS.IBLL;
using EOS.BLL;
using EOS.EntWebDemo.Models.Leave;
using System.Reflection;
using EOS.Common;

namespace EOS.EntWebDemo.Controllers
{
    public class LeaveStepsController : Controller
    {
        private DataModelContainer db = new DataModelContainer();

        #region "页面关联处理"
        public ActionResult LinkPage(int? id)
        {
            List<R_LeaveStep_PageInfo> rList = db.R_LeaveStep_PageInfo.Where(r => r.LeaveStepID == id).ToList();
            List<LinkPageView> linkPageViews = new List<LinkPageView>();
            ViewBag.leaveStepID = id;

            if (rList.Count > 0)
            {
                foreach (var r in rList)
                {
                    LinkPageView linkPageView = new LinkPageView();
                    linkPageView.ID = r.ID;
                    linkPageView.LeaveStepID = r.LeaveStepID;
                    linkPageView.LeaveStepName = db.LeaveStep.Find(new object[] { r.LeaveStepID }).StepName;
                    linkPageView.PageInfoID = r.PageInfoID;
                    linkPageView.PageInfoName = db.PageInfo.Find(new object[] { r.PageInfoID }).PageName;
                    linkPageView.PageUrl = db.PageInfo.Find(new object[] { r.PageInfoID }).PageUrl;
                    linkPageView.ReturnKBN = r.ReturnKBN;
                    linkPageViews.Add(linkPageView);
                }
            }
            return View(linkPageViews);
        }

        public string GetPageUrl(int? id)
        {
            ViewBag.leaveStepID = id;
            List<PageInfo> pageInfos = db.PageInfo.Where(p => p.ID == id).ToList();
            if (pageInfos.Count > 0)
            {
                return pageInfos[0].PageUrl;
            }
            else
            {
                return string.Empty;
            }
        }

        public ActionResult AddLinkPage(int? id)
        {
            //页面名称下拉框绑定
            List<PageInfo> pageInfos = db.PageInfo.ToList();
            SelectList pageNameList = new SelectList(pageInfos, "ID", "PageName");
            ViewBag.pageInfos = pageInfos;
            ViewBag.pageNameList = pageNameList.AsEnumerable();

            //View设置
            LinkPageView linkPageView = new LinkPageView();
            linkPageView.LeaveStepID = (int)id;
            linkPageView.LeaveStepName = db.LeaveStep.Find(new object[] { id }).StepName;
            return View(linkPageView);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteLinkPage(int id)
        {
            R_LeaveStep_PageInfo r = db.R_LeaveStep_PageInfo.Find(id);
            db.R_LeaveStep_PageInfo.Remove(r);
            db.SaveChanges();

            return RedirectToAction("LinkPage", "LeaveSteps", new { id = r.LeaveStepID });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddLinkPage(LinkPageView linkPageView)
        {
            if (ModelState.IsValid)
            {
                R_LeaveStep_PageInfo r = new R_LeaveStep_PageInfo();
                r.LeaveStepID = linkPageView.LeaveStepID;
                r.PageInfoID = linkPageView.PageInfoID;
                r.ReturnKBN = linkPageView.ReturnKBN;

                db.R_LeaveStep_PageInfo.Add(r);
                db.SaveChanges();
                return RedirectToAction("LinkPage", "LeaveSteps", new { id = r.LeaveStepID });
            }

            return AddLinkPage(linkPageView.LeaveStepID);
        }
        #endregion

        // GET: LeaveSteps
        public ActionResult Index()
        {
            return View(db.LeaveStep.ToList().OrderBy(x => x.StepOrder));
        }

        // GET: LeaveSteps/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            LeaveStep leaveStep = db.LeaveStep.Find(id);
            if (leaveStep == null)
            {
                return HttpNotFound();
            }
            return View(leaveStep);
        }

        // GET: LeaveSteps/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: LeaveSteps/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,StepName,StepOrder")] LeaveStep leaveStep)
        {
            if (ModelState.IsValid)
            {
                db.LeaveStep.Add(leaveStep);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(leaveStep);
        }

        // GET: LeaveSteps/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            LeaveStep leaveStep = db.LeaveStep.Find(id);
            if (leaveStep == null)
            {
                return HttpNotFound();
            }
            return View(leaveStep);
        }

        // POST: LeaveSteps/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,StepName,StepOrder")] LeaveStep leaveStep)
        {
            if (ModelState.IsValid)
            {
                db.Entry(leaveStep).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(leaveStep);
        }

        // GET: LeaveSteps/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            LeaveStep leaveStep = db.LeaveStep.Find(id);
            if (leaveStep == null)
            {
                return HttpNotFound();
            }
            return View(leaveStep);
        }

        // POST: LeaveSteps/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            LeaveStep leaveStep = db.LeaveStep.Find(id);
            db.LeaveStep.Remove(leaveStep);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
